<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Config extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        if($_SESSION['user']['status'] == 3){
            redirect(site_url('home'));
        }
    }

    public function index(){
        $this->db->where('id',1);
        $res = $this->config_mdl->get_one();

        //获取文章标题
        $this->load->model('article_mdl');
        $this->db->select('id,title');
        $this->db->where('id',$res->article_id);
        $article = $this->article_mdl->get_one();
        $this->_view('index',array('res'=>$res,'article' => $article));
    }

    /**
     * 系统设置
     */
    public function update(){
        if(!$this->input->is_ajax_request()){
            show_error('参数错误！');
        }

        $post = $this->input->post();

        if(empty($post['title'])){
            die('站点标题不能为空');
        }
        if(!preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/",$post['email'])){
            die('联系邮箱格式错误，请填写正确的邮箱地址，如 zbzmall@163.com');
        }

        if(empty($post['article_id'])){
            die('请填写用户推广文章');
        }

        $data = array(
            'title' => $post['title'],
            'email' => $post['email'],
            'status' => intval($post['status']),
            'article_id' => intval($post['article_id'])
        );

        $this->db->where('id',1);
        $res = $this->config_mdl->update($data);

        unset($_SESSION['config']);
        $this->config_mdl->set_sess();
        echo $res === true ? 1 : '操作失败！';
    }

}

/* End of file Config.php */
/* Location: ./application/controllers/Config.php */